<?php


namespace App\Services;


use App\Constants\AdvConfigConstant;
use App\Constants\IntegralLogTypeConstant;
use App\Constants\SignConstant;
use App\Exceptions\BusinessException;
use App\Models\MemberIntegralLog;
use App\Models\MemberSignLog;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class SignService
{
    public function sign(string $miniType, int $memberId): ?array
    {
        $log = MemberSignLog::query()
            ->where('member_id', '=', $memberId)
            ->where('mini_type', '=', $miniType)
            ->orderByDesc('id')
            ->first();

        if ($log && date('Y-m-d') == date('Y-m-d', strtotime($log->created_at))) {
            return null;
        }

        $day = 1;

        if ($log && date('Y-m-d', strtotime($log->created_at)) == date('Y-m-d', strtotime('-1 day'))) {
            $day = $log->continue_day + 1;
        }

        DB::beginTransaction();

        try {
            MemberSignLog::create([
                'member_id'    => $memberId,
                'continue_day' => $day,
                'year'         => date('Y'),
                'month'        => date('m'),
                'mini_type'    => $miniType,
            ]);

            $settings = getMiniSettings($miniType, SignConstant::GROUP);
            $dayIntegral = Arr::get($settings, SignConstant::DAY_INTEGRAL);
            $severDayIntegral = Arr::get($settings, SignConstant::CONTINUOUS_SEVEN_DAY_INTEGRAL);

            if ($dayIntegral > 0) {
                app(IntegralService::class)->store($miniType, $memberId, $dayIntegral, IntegralLogTypeConstant::TYPE_BY_SIGN, '日常签到');
            }

            if ($day >= 7 && $severDayIntegral > 0) {
                app(IntegralService::class)->store($miniType, $memberId, $severDayIntegral, IntegralLogTypeConstant::TYPE_BY_SIGN, '连续签到7天');
            }

            DB::commit();
        } catch (BusinessException $e) {
            DB::rollBack();
            throw $e;
        } catch (\Exception $e) {
            DB::rollBack();
            log_exception('签到异常', $e);
            throw new BusinessException('签到失败');
        }

        return [
            'integral' => $dayIntegral,
            'title'    => '签到成功',
            'content'  => sprintf('恭喜您,本次签到获得%s积分!', $dayIntegral)
        ];
    }

    public function video(int $memberId, string $advId, string $miniType): ?array
    {
        $settings = getMiniSettings($miniType, AdvConfigConstant::ADV_GROUP);

        $status = Arr::get($settings, AdvConfigConstant::SIGN_VIDEO_ADV_STATUS);
        $videoId = Arr::get($settings, AdvConfigConstant::INCENTIVE_ADV_ID);

        $signSetting = getMiniSettings($miniType, SignConstant::GROUP);
        $integral = Arr::get($signSetting, SignConstant::SIGN_VIDEO_INTEGRAL);

        if ($advId != $videoId) {
            throw new BusinessException('广告位id不匹配');
        }

        $exists = MemberIntegralLog::query()->where([
            'member_id' => $memberId,
            'type'      => IntegralLogTypeConstant::TYPE_BY_SIGN_VIDEO,
            'mini_type' => $miniType,
        ])->whereDate('created_at', '=', date('Y-m-d'))->exists();

        if (!$status || $integral <= 0 || $exists) {
            return null;
        }

        try {
            DB::beginTransaction();

            app(IntegralService::class)->store($miniType, $memberId, $integral, IntegralLogTypeConstant::TYPE_BY_SIGN_VIDEO, '观看签到视频');

            DB::commit();
        } catch (BusinessException $e) {
            DB::rollBack();
            throw $e;
        } catch (\Exception $e) {
            DB::rollBack();
            log_exception('观看签到视频奖励积分异常', $e);
            throw new BusinessException('观看签到视频奖励积分异常');
        }

        return ['integral' => $integral];
    }
}
